const { features } = require('process');

module.exports = function (app, api_prefix = '/api/system') {
    
    const log4js = require('log4js');
    const nodeDiskInfo = require('node-disk-info');


    const logger = log4js.getLogger();
    log4js.getLogger().debug(`Startup system api_prefix=${api_prefix}`);

    // cgi, common gateway interface 通用网关接口
    // api, application programming  interface 应用程序接口
    app.use(`${api_prefix}/:purpose`, function (req, res, next) {
        var params = req.method == "GET" ? req.query : req.body;
        var headers = req.headers;
        switch (req.params.purpose) {
            case "disk": {
                try {
                    const disks = nodeDiskInfo.getDiskInfoSync();
                    sendResponse(res, disks);
                } catch (e) {
                    sendResponse(res, e.toString(), 500);
                }
                break;
            }
            default: {
                sendResponse(res, {
                    code: 404,
                    message: `not found purpose=${req.params.purpose}`,
                }, 404);
                break;
            }
        }
    });

    function sendResponse(res, data, statusCode = 200) {
        res.set({
            'Content-Type': 'application/json',
        });
        res.status(statusCode);
        res.send({
            data,
        });
    }
}